/**
 * 作者: wgx
 * 日期: 2025-03-31 10:30:51
 * 备注: 用户中餐厅模块表信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.act.ActTopRanking;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.rank.RankConstant;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.*;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonObject;

public class UserRestaurantInfo extends UserRestaurantInfoModel {
    public UserRestaurantInfo() {
        super();
    }

    public void reset() {
        recoverReceptionNum();
    }

    public JsonObject insert() {
        putReceptionNum(RestaurantManager.getReceptionMaxByScore(getScore()));
        putReceptionTime(TimeUtils.getCurTime());
        return super.insert();
    }

    // 恢复接待次数
    public void recoverReceptionNum() {
        long curTime = TimeUtils.getCurTime();
        int max = RestaurantManager.getReceptionMaxByScore(getScore());// 自动恢复的体力上限
        if (getReceptionNum() >= max) {// 此时体力已经达到上限
            return;
        }
        int cdtime = AllParam.RESTAURANT_PHUSICAL_RECOVERY_TIME * TimeUtils.MINUTE;// 体力恢复的CD时间
        long diff = TimeUtils.getTimeDiff(getReceptionTime());
        if (max == 0 || cdtime == 0 || diff < cdtime) {
            return;// 不需要执行恢复逻辑
        }

        int addNum = (int) Math.floor((double) diff / cdtime);  // 恢复体力点数
        if (getReceptionNum() + addNum < max) {
            putReceptionTime(getReceptionTime() + cdtime * addNum);// // 恢复了n点, 标记时间就加N个cdtime
        } else {
            addNum = max - getReceptionNum();
            putReceptionTime(curTime);
        }
        addReceptionNum(addNum);
        update();
    }

    // 扣除接待次数
    public void subReceptionNum(int value) {
        int balance = getReceptionNum();
        if (value > balance) {
            value = balance;
        }
        int max = RestaurantManager.getReceptionMaxByScore(getScore());// 自动恢复的体力上限
        if (balance >= max && balance - value < max) {// // 原来的体力超过上限,且使用后低于下限,开始cd倒计
            putReceptionTime(TimeUtils.getCurTime());
        }
        super.addReceptionNum(-value);
    }

    // 记录日志并增加积分
    public void addScore(PBUserData.Builder pbUserData, UserInfo userInfo, int num, Object... params) {
        if (num == 0) {
            return;
        }
        long balance = getScore();// 当前余额
        int maxOld = RestaurantManager.getReceptionMaxByScore(getScore());// 原本的接待次数上限
        long dbid = 0;
        MotuLogManager.logItemIncome(userInfo, ItemId.RESTAURANT_SCORE, dbid, balance, num, params);
        super.addScore(num);
        int maxNew = RestaurantManager.getReceptionMaxByScore(getScore());// 新的上限
        if (maxNew > maxOld) {
            addReceptionNum(maxNew - maxOld);// 上限增加，添加次数
        }

        MainTaskManager.updateTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_222, getScore());// 餐厅人气达到{0}
        RankManager.updateRank(RankConstant.SERVER_RANK_RESTAURANT, userInfo.getServerId(), userInfo.getId(), getScore(), "0");// 全量更新排行榜
        RankActManager.addUserrank(userInfo.getServerId(), ActTopRanking.RANK_TYPE_RESTAURANT_SCORE, userInfo, num, true);
        RankActManager.addUserCrossRank(userInfo, userInfo.getServerId(), ActTopRanking.RANK_TYPE_RESTAURANT_SCORE, num, true);
    }
}
